package cn.tedu.tea.admin.server.content.service.impl;

import cn.tedu.tea.admin.server.common.ex.ServiceException;
import cn.tedu.tea.admin.server.common.pojo.vo.PageData;
import cn.tedu.tea.admin.server.common.web.ServiceCode;
import cn.tedu.tea.admin.server.content.dao.persist.repository.ICommentRepository;
import cn.tedu.tea.admin.server.content.pojo.entity.Comment;
import cn.tedu.tea.admin.server.content.pojo.param.CommentAddNewParam;
import cn.tedu.tea.admin.server.content.pojo.vo.CommentListItemVO;
import cn.tedu.tea.admin.server.content.pojo.vo.CommentStandardVO;
import cn.tedu.tea.admin.server.content.service.ICommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 处理评论数据的业务实现类
 *
 * @author java@tedu.cn
 * @version 1.0
 */
@Slf4j
@Service
public class CommentServiceImpl implements ICommentService {

    @Value("${tea-store.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private ICommentRepository commentRepository;

    public CommentServiceImpl() {
        log.info("创建业务对象：CommentServiceImpl");
    }

    @Override
    public void addNew(CommentAddNewParam commentAddNewParam) {
        log.debug("开始处理【新增评论】的业务，参数：{}", commentAddNewParam);

        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddNewParam, comment);
        int rows = commentRepository.insert(comment);
        if (rows != 1) {
            String message = "新增评论失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void passCheck(Long id) {
        log.debug("开始处理【审核通过评论】的业务，参数：{}", id);
        updateCheckById(id, 1);
    }

    @Override
    public void cancelCheck(Long id) {
        log.debug("开始处理【取消审核通过评论】的业务，参数：{}", id);
        updateCheckById(id, 0);
    }

    @Override
    public void setDisplay(Long id) {
        log.debug("开始处理【显示评论】的业务，参数：{}", id);
        updateDisplayById(id, 1);
    }

    @Override
    public void setHidden(Long id) {
        log.debug("开始处理【不显示评论】的业务，参数：{}", id);
        updateDisplayById(id, 0);
    }

    @Override
    public PageData<CommentListItemVO> list(Integer pageNum) {
        log.debug("开始处理【查询评论数据列表】的业务，页码：{}", pageNum);
        PageData<CommentListItemVO> pageData = commentRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<CommentListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询评论数据列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<CommentListItemVO> pageData = commentRepository.list(pageNum, pageSize);
        return pageData;
    }

    @Override
    public PageData<CommentListItemVO> listByArticleId(Long articleId, Integer pageNum) {
        log.debug("开始处理【根据文章查询评论列表】的业务，页码：{}", pageNum);
        PageData<CommentListItemVO> pageData = commentRepository.listByArticleId(articleId, pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<CommentListItemVO> listByArticleId(Long articleId, Integer pageNum, Integer pageSize) {
        log.debug("开始处理【根据文章查询评论列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<CommentListItemVO> pageData = commentRepository.listByArticleId(articleId, pageNum, pageSize);
        return pageData;
    }

    private void updateCheckById(Long id, Integer checkState) {
        CommentStandardVO currentComment = commentRepository.getStandardById(id);
        if (currentComment == null) {
            String message = "将评论的审核状态修改为【" + CHECK_TEXT[checkState] + "】失败，评论数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        if (currentComment.getCheckState() == checkState) {
            String message = "将评论的审核状态修改为【" + CHECK_TEXT[checkState] + "】失败，此评论已经处于" + CHECK_TEXT[checkState] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        Comment updateComment = new Comment();
        updateComment.setId(id);
        updateComment.setCheckState(checkState);
        int rows = commentRepository.update(updateComment);
        if (rows != 1) {
            String message = "将评论的审核状态修改为【" + CHECK_TEXT[checkState] + "】失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    private void updateDisplayById(Long id, Integer displayState) {
        CommentStandardVO currentComment = commentRepository.getStandardById(id);
        if (currentComment == null) {
            String message = "将评论的显示状态修改为【" + DISPLAY_TEXT[displayState] + "】失败，评论数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        if (currentComment.getDisplayState() == displayState) {
            String message = "将评论的显示状态修改为【" + DISPLAY_TEXT[displayState] + "】失败，此评论已经处于" + DISPLAY_TEXT[displayState] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        Comment updateComment = new Comment();
        updateComment.setId(id);
        updateComment.setDisplayState(displayState);
        int rows = commentRepository.update(updateComment);
        if (rows != 1) {
            String message = "将评论的显示状态修改为【" + DISPLAY_TEXT[displayState] + "】失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
}